//
//  VVViewController.m
//  ComponentLinkDemo
//
//  Created by wenchao on 17/3/2.
//  Copyright © 2017年 zhenby. All rights reserved.
//

#import "VVViewController.h"
#import "CLActionCenter.h"
#import "CLActionEvent.h"
#import "CLPActionPerformer.h"
#import "VVComponent.h"
#import "VVActionIdentifier.h"

#import "VVCloseComponent.h"

@interface VVViewController ()

@property (nonatomic,strong) CLActionCenter* actionCenter;

@end

@implementation VVViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self p_loadComponents];
    }
    return self;
}

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)p_loadComponents
{
    [self setValue:self.actionCenter forKey:@"actionPerformer"];
    VVComponent* component = [[VVComponent alloc] initWithParentController:self];
    [self addSubcomponent:component];
    
    VVCloseComponent* closeComp = [[VVCloseComponent alloc] initWithParentController:self];
    [self addSubcomponent:closeComp];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    for (VVComponent* comp in self.subComponents) {
        [comp loadView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (nullable NSDictionary*)performActionEvent:(nonnull CLActionEvent*)action
{
    if ([action.actionName isEqualToString:kCloseServiceAction]) {
        
        return [self.subComponents.lastObject actionServiceWithEvent:action];
    }
    
    return [self.supercomponent performActionEvent:action];
}

#pragma mark -
- (CLActionCenter*)actionCenter
{
    if (!_actionCenter) {
        _actionCenter = [[CLActionCenter alloc] initWithRootComponent:self];
    }
    return _actionCenter;
}

@end
